import Vue from 'vue'
import axios from 'axios'
import VueRouter from 'vue-router'
import routes from './router/router'
import store from './store/'
import './config/rem' //以iphone5为基准   即html{font-size:20px}；但是开发时以iphone6为设计稿的话  ide就要设置为23.5px
import FastClick from 'fastclick'
import 'mint-ui/lib/style.css'
import Mint from 'mint-ui';
import {
	Indicator
} from 'mint-ui';
import md5 from 'js-md5';



// import myToast from './plugins/myToast/index.js'
import myLoading from './plugins/myLoading/index.js'

window.eventBus = new Vue();
if ('addEventListener' in document) {
	document.addEventListener('DOMContentLoaded', function() {
		FastClick.attach(document.body);
	}, false);

}

Vue.use(Mint);
//
// Vue.use(myToast);
Vue.use(myLoading);

const router = new VueRouter({
	routes,
	mode: 'hash',
	strict: process.env.NODE_ENV !== 'production',
	scrollBehavior(to, from, savedPosition) {
		if (savedPosition) {
			return savedPosition
		} else {
			if (from.meta.keepAlive) {
				from.meta.savedPosition = document.body.scrollTop;
			}
			return {
				x: 0,
				y: to.meta.savedPosition || 0
			}
		}
	}
})



router.beforeEach((to, from, next) => {
	/* 路由发生变化修改页面title */
	if (to.meta.title) {
		document.title = to.meta.title
	}
	let userInfo = JSON.parse(sessionStorage.getItem('userInfo'));
	if (to.meta.auth) //需要登录
	{
		if (userInfo) { //已经登录了
			if (from.path == '/company') {
				sessionStorage.setItem('CompanyUpdate', 'yes')
			} else {
				sessionStorage.setItem('CompanyUpdate', 'none')
			}

			next()
		} else {
			next({
				path: '/login',
			})
			// router.replace({
			//   path: '/login'
			// })
			// next()
		}
	} else {
		next();
	}
})
Vue.use(VueRouter)


/**
 * @description 全局请求配置
 */
if (process.env.NODE_ENV !== 'production') {
	axios.defaults.baseURL = 'http://47.103.102.210:9081/'; //开发
} else {
	axios.defaults.baseURL = 'http://47.103.102.210:9081/'; //测试
	// axios.defaults.baseURL ='https://www.m12315.com/mgjapi/';//正式
}

// axios.defaults.headers.common['Authorization'] = 'AUTH_TOKEN';
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// 添加一个请求拦截器
axios.interceptors.request.use(function(config) {
	// Do something before request is sent
	Indicator.open({
		spinnerType: 'fading-circle'
	});
	// mainvm.$myLoadingShow();
	if (sessionStorage.getItem('userInfo')) {
		var token = JSON.parse(sessionStorage.getItem('userInfo')).token;
		if (token) {
			config.headers['token'] = token;
		}
	}
	return config;
}, function(error) {
	// Do something with request error
	return Promise.reject(error);
});
// 添加一个响应拦截器
axios.interceptors.response.use(function(response) {
	// Do something with response data
	Indicator.close();
	// mainvm.$myLoadingHide()
  if(response.data.msg=='not login'){
    mainvm.$mint.Toast({
    	message:'登陆失效',
    	iconClass: 'iconfont icon-cuo',
    	duration: 1500
    });
    setTimeout(()=>{
      mainvm.$router.replace({
					path: '/login'
				})
    },1500)
  }
	return response;
}, function(error) {
	// Do something with response error
	return Promise.reject(error);
});
Vue.prototype.$axios = axios;
Vue.prototype.$md5 = md5;
Vue.prototype.$mint = Mint;



var mainvm = new Vue({
	router,
	store,
}).$mount('#app')
